﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Collections;

using Wind.Utils;
using Wind.Utils.DataAccess;
using Wind.Framework;
using Wind.Framework.Config;
using Wind.Framework.Entity;
using Wind.MultimediaStation.WebService.Entity;
using Wind.MultimediaStation.Common;


namespace Wind.MultimediaStation.WebService
{
    public class MultimediaStationBusiness : IMultimediaStationBusiness
    {

        private IDBHelper _dbHelper;
        private const string SP_GetChannelList = "SP_MultimediaStation_WFSP_Channel_GetList";
        private const string SP_GetPlayOrderListByChannelID = "SP_MultimediaStation_GetPlayOrderListByChannelID";
        private const string SP_GetApplicationConfigListByUserID = "SP_MultimediaStation_GetApplicationConfigListByUserID";
        private const string SP_GetPlayOrderMaterialListByOrderID = "SP_MultimediaStation_GetPlayOrderMaterialListByOrderID";
        private const string SP_GetPlayOrderActsListByOrderID = "SP_MultimediaStation_GetPlayOrderActsListByOrderID";
        private const string SP_MultimediaStation_HasChannel = "SP_MultimediaStation_HasChannel";
        //
        public MultimediaStationBusiness()
        {
            _dbHelper = DBFactoryHelper.GetDatabase(DatabaseType.SqlServer, FrameworkSection.GetConnectionString("ConnectionString"));
        }
        /// <summary>
        /// 获取频道列表
        /// </summary>
        /// <returns></returns>
        public List<OChannelInfo> GetChannelList()
        {
            List<OChannelInfo> data = new List<OChannelInfo>();
            try
            {
                DataTable dt = _dbHelper.ExcuteDataTable(SP_GetChannelList);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        OChannelInfo info = new OChannelInfo();
                        info.ChannelID = ParseHelper.Parse<int>(dr["ChannelID"]);
                        info.ChannelName = dr["ChannelName"].ToString();
                        info.ThumbnailSrc = dr["ThumbnailSrc"].ToString().Replace("../", "");
                        info.RefreshPlayOrderInterval = ParseHelper.Parse<int>(dr["RefreshPlayOrderInterval"]) * 60;
                        //
                        data.Add(info);
                    }
                }
                return data;
            }
            catch (Exception ex)
            {
                LogHelper.Write("MultimediaStationService GetChannelList Exception:", ex);
                return null;
            }
        }

        /// <summary>
        /// 是否有新频道
        /// </summary>       
        /// <returns></returns>
        public bool HasNewChannnel()
        {
            Hashtable ht = new Hashtable();
            ht.Add("@Status", ChannelStatus.New);
            string str = _dbHelper.ExecNonQuery(SP_MultimediaStation_HasChannel, ht, true);
            return str == "1";
        }
        /// <summary>
        /// 获取频道的串联单
        /// </summary>
        /// <param name="ChannelID">频道ChannelID</param>
        /// <returns></returns>
        public List<OPlayOrderInfo> GetPlayOrderListByChannelID(int channelID)
        {
            List<OPlayOrderInfo> data = new List<OPlayOrderInfo>();
            try
            {
                Hashtable hsh = new Hashtable();
                hsh.Add("@ChannelID", channelID);
                DataTable dt = _dbHelper.ExcuteDataTable(SP_GetPlayOrderListByChannelID, hsh);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        OPlayOrderInfo info = new OPlayOrderInfo();
                        info.OrderID = ParseHelper.Parse<int>(dr["OrderID"]);
                        info.OrderBeginDate = ParseHelper.Parse<DateTime>(dr["PlayStartTime"]).ToString();
                        info.OrderEndDate = ParseHelper.Parse<DateTime>(dr["PlayEndTime"]).ToString();
                        info.IsLoaded = false;
                        //串联单素材信息
                        info.ActDownList = CommonBusiness.QueryActDownInfByOrderID(info.OrderID);

                        //-------------------------------
                        Hashtable ht = new Hashtable();
                        ht.Add("@OrderID", info.OrderID);
                        DataTable dtOrder = _dbHelper.ExcuteDataTable(SP_GetPlayOrderActsListByOrderID, ht);
                        if (dtOrder != null && dtOrder.Rows.Count > 0)
                        {
                            foreach (DataRow drOrder in dtOrder.Rows)
                            {
                                OActInfo infoAct = new OActInfo();
                                infoAct.ActID = ParseHelper.Parse<int>(drOrder["ActID"]);
                                infoAct.TemplateType = ParseHelper.Parse<int>(drOrder["TemplateType"]);
                                //

                                if (infoAct.TemplateType == (int)TemplateType.PicNews)
                                {
                                    List<OMaterialInfo> lstMaterial;

                                    lstMaterial = CommonBusiness.QueryMaterialPictureListByActID(info.OrderID, infoAct.ActID);

                                    OActDownInfo oActDown;
                                    foreach (OMaterialInfo o in lstMaterial)
                                    {
                                        oActDown = new OActDownInfo();
                                        oActDown.DownloadTime = 300;
                                        oActDown.DownloadURL = o.MaterialName;
                                        oActDown.DownloadType = 2;
                                        info.ActDownList.Add(oActDown);
                                    }
                                }
                            }
                        }
                        //--------------------
                        data.Add(info);
                    }
                }
                return data;
            }
            catch (Exception ex)
            {
                LogHelper.Write("MultimediaStationService GetPlayOrderListByChannelID Exception", ex);
                return null;
            }
        }
        /// <summary>
        /// 根据串联单获取节目列表
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public List<OActInfo> GetActInfoListByID(int orderID)
        {
            return CommonBusiness.QueryActInfoByOrderID(orderID);
        }
        /// <summary>
        /// 根据城市名称获取天气预报
        /// </summary>
        /// <param name="cityName"></param>
        /// <returns></returns>
        public OWeather GetWeatherbyCityName(string cityName)
        {
            return MultimediaStationGoogleWeatherBusiness.GetWeatherbyCityName(cityName);           
        }

        /// <summary>
        /// 写log 
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
       public void WriterErrorLog(string message)
        {
            LogHelper.Write("MultimediaStationService Client WriterError Exception:", new Exception(message));
        }
    }
}
